﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
namespace TS_XY_Train_Repaire.UserCon.Locomotive.Technology
{
    public partial class uc_Locomotive_MainBase_WorkFlow_ElectricWorkMain : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID , _LoadType,_TCID,_TTID,_RPID,_Name = "";
        public string _TDID = "2";
        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            if (_LoadType == "Add")
            {
                strSQL = "insert into TS_TR_PS_Work_Main(f_name,f_rpid,f_type,f_funcid_1,f_funcid_2,f_workerid) values('" + tb_Name.Text.Trim() + "'," + cb_TrainRP_List.SelectedValue.ToString() + "," + _TDID + "," + cb_TrainType_List.SelectedValue.ToString() + "," + cb_DepSys_Type.SelectedValue.ToString() + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
            }
            if (_LoadType == "Del")
            {
                 strSQL = "update TS_TR_PS_Work_Main set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id="+_LoadID;
            }
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;
        }

        private void cb_TrainType_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainType_List.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_DepSys_Type, "select f_id,f_name from TS_S_T_P_TrainDepSys_Type where f_del=0 and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_del=0 and f_level like '%," + _TDID + ",%' and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString() + ") and f_ttid=" + cb_TrainType_List.SelectedValue.ToString());
            }
        }

        private void cb_DepSys_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }
        private void _SetButtonEnabled()
        {
            if (tb_Name.Text.Trim().Length > 0 && cb_TrainClass_List.SelectedIndex > -1 && cb_TrainType_List.SelectedIndex > -1 && cb_TrainRP_List.SelectedIndex > -1 && cb_DepSys_Type.SelectedIndex > -1)
            {
                bt_Sure.Enabled = true;
            }
            else
            {
                bt_Sure.Enabled = false;
            }
            tb_Name.Text = cb_DepSys_Type.Text.Trim() + " " + cb_TrainRP_List.Text.Trim() + "作业流程";
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }

        private void uc_Locomotive_MainBase_WorkFlow_ElectricWorkMain_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(cb_TrainClass_List, "select f_id,f_name from TS_S_T_P_TrainClass where f_del=0");
            if (cb_TrainClass_List.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_TrainRP_List, "select f_id,f_name from TS_S_T_P_Train_RP where f_del=0 and f_id in (select f_rpid from TS_S_T_P_Train_RP_TC where f_del=0 and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString() + " )");
                TS_Win_Class.FillListTypeAct(cb_TrainType_List, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString());
                if (cb_TrainType_List.SelectedIndex > -1)
                {
                    TS_Win_Class.FillListTypeAct(cb_DepSys_Type, "select f_id,f_name from TS_S_T_P_TrainDepSys_Type where f_del=0 and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_del=0 and f_level like '%," + _TDID + ",%' and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString() + ") and f_ttid=" + cb_TrainType_List.SelectedValue.ToString());
                }
            }
            if (_LoadType=="Del")
            {
                cb_TrainClass_List.SelectedValue = _TCID;
                Thread.Sleep(100);
                tb_Name.Text =_Name;
                bt_Sure.Text = "确定删除";
                cb_TrainType_List.SelectedValue = _TTID;
                cb_TrainRP_List.SelectedValue = _RPID;
            }
            if (_LoadType == "Add")
            {
                bt_Sure.Text = "确定新增";
                bt_Sure.Enabled = false;
            }
        }

        private void cb_TrainClass_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_TrainClass_List.SelectedIndex > -1)
            {
                TS_Win_Class.FillListTypeAct(cb_TrainRP_List, "select f_id,f_name from TS_S_T_P_Train_RP where f_del=0 and f_id in (select f_rpid from TS_S_T_P_Train_RP_TC where f_del=0 and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString() + " )");
                TS_Win_Class.FillListTypeAct(cb_TrainType_List, "select f_id,f_name from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString());
                if (cb_TrainType_List.SelectedIndex > -1)
                {
                    TS_Win_Class.FillListTypeAct(cb_DepSys_Type, "select f_id,f_name from TS_S_T_P_TrainDepSys_Type where f_del=0 and f_tdid in (select f_id from TS_S_T_P_TrainDepSys where f_del=0 and f_level like '%," + _TDID + ",%' and f_tcid=" + cb_TrainClass_List.SelectedValue.ToString() + ") and f_ttid=" + cb_TrainType_List.SelectedValue.ToString());
                }
            }
        }

        public uc_Locomotive_MainBase_WorkFlow_ElectricWorkMain()
        {
            InitializeComponent();
        }
    }
}